package com.xmb.quick.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.xmb.quick.annotation.Log;
import com.xmb.quick.constant.R;
import com.xmb.quick.entity.SysDept;
import com.xmb.quick.service.ISysDeptService;

import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/department")
public class DeptController {

	@Autowired
	private ISysDeptService iSysDeptService;
	
	@GetMapping(value = "/index")
	public String index() {
		return "dept/index";
	}
	
	@GetMapping("/addHtml")
	public String addHtml(String id,Model model) {
		model.addAttribute("pid", id);
		return "dept/add";
	}
	
	@GetMapping("/editHtml")
	public String editHtml(String id,Model model) {
		SysDept sysDept = iSysDeptService.selectInfo(new EntityWrapper<SysDept>().eq("id", id));
		model.addAttribute("dept", sysDept);
		return "dept/edit";
	}
	
	@GetMapping("/list")
	@ResponseBody
	public Object list() {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("code", 0);
		resultMap.put("msg", "ok");
		List<SysDept> list = iSysDeptService.selectInfoList(null);
		List<Map<String, Object>> list1 = new ArrayList<>();
		Map<String, Object> map = null;
		for (SysDept sysDept : list) {
			map = new HashMap<String, Object>();
			map.put("id", sysDept.getId());
			if(StringUtils.isEmpty(sysDept.getPid())) {
				map.put("pid", "-1");
			}else {
				map.put("pid", sysDept.getPid());
			}
			map.put("deptCode", sysDept.getDeptCode());
			map.put("deptName", sysDept.getDeptName());
			list1.add(map);
		}
		resultMap.put("data", list1);
		resultMap.put("count", list.size());
		return resultMap;
	}

	@Log("添加部门")
	@ApiOperation(value = "添加部门")
	@PostMapping("/add")
	@ResponseBody
	public R add(SysDept sysDept) {
		try {
			SysDept dept = iSysDeptService.selectInfo(new EntityWrapper<SysDept>().eq("dept_code", sysDept.getDeptCode()));
			if(dept != null) {
				return R.error("部门编码已存在，无法添加！");
			}
			iSysDeptService.add(sysDept);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统异常");
		}
		return R.ok();
	}
	
	@Log("修改部门")
	@PostMapping("/edit")
	@ResponseBody
	public R edit(SysDept sysDept) {
		try {
			iSysDeptService.edit(sysDept);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统异常");
		}
		return R.ok();
	}
	
	@Log("删除部门")
	@PostMapping("/delete")
	@ResponseBody
	public R delete(String id) {
		try {
			iSysDeptService.remove(new EntityWrapper<SysDept>().eq("id", id).or().eq("pid", id));
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统异常");
		}
		return R.ok();
	}
}